package com.sojson.service.impl;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sojson.config.exception.TransException;
import com.sojson.constant.ConstantCache;
import com.sojson.dao.BrokerageDao;
import com.sojson.entity.po.Brokerage;
import com.sojson.service.BrokerageService;
import com.sojson.util.ArithUtil;
import com.sojson.util.DateUtil;
import com.sojson.util.StringUtil;
import com.sojson.util.cache.CacheUtil;

/**
 * 分佣信息Service实现类
 * 
 * @author liu
 * @date 2021-06-29
 */
@Service
public class BrokerageServiceImpl implements BrokerageService {

    @Resource
    private BrokerageDao brokerageDaoImpl;

    /**
     * 单条插入
     * 
     * @param brokerage
     * @return
     */
    @Override
    public int insert(Brokerage brokerage) {
        int insert = brokerageDaoImpl.insert(brokerage);
        return insert;
    }

    /**
     * 批量插入
     * 
     * @param brokerage
     * @return
     */
    @Override
    public int insertList(Brokerage brokerage) {
        int insertList = brokerageDaoImpl.insertList(brokerage.getBrokerages());
        return insertList;
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param brokerage
     * @return
     */
    @Override
    public int deleteByDto(Brokerage brokerage) {
        int deleteByDto = brokerageDaoImpl.deleteByDto(brokerage);
        return deleteByDto;
    }

    /**
     * 统计全部数量
     * 
     * @return
     */
    @Override
    public int countAll() {
        int countAll = brokerageDaoImpl.countAll();
        return countAll;
    }

    /**
     * 查询所有
     * 
     * @return
     */
    @Override
    public List<Brokerage> listAll() {
        List<Brokerage> findAll = brokerageDaoImpl.listAll();
        return findAll;
    }

    /**
     * 根据对象统计数量
     * 
     * @param brokerage
     * @return
     */
    @Override
    public int countByDto(Brokerage brokerage) {
        int countByDto = brokerageDaoImpl.countByDto(brokerage);
        return countByDto;
    }

    /**
     * 根据对象查询
     * 
     * @param brokerage
     * @return
     */
    @Override
    public List<Brokerage> listByDto(Brokerage brokerage) {
        List<Brokerage> findByDto = brokerageDaoImpl.listByDto(brokerage);
        return findByDto;
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     */
    @Override
    public int deleteById(Integer id) {
        int deleteById = brokerageDaoImpl.deleteById(id);
        return deleteById;
    }

    /**
     * 通过id数组删除(物理删除)
     * 
     * @param ids
     * @return
     */
    @Override
    public int deleteByIds(Integer[] ids) {
        int deleteByIds = brokerageDaoImpl.deleteByIds(ids);
        return deleteByIds;
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param brokerage
     * @return
     */
    @Override
    public int updateAllById(Brokerage brokerage) {
        int updateAllById = brokerageDaoImpl.updateAllById(brokerage);
        return updateAllById;
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param brokerage
     * @return
     */
    @Override
    public int updateAllByIdList(Brokerage brokerage) {
        int updateAllByIdList = brokerageDaoImpl.updateAllByIdList(brokerage);
        return updateAllByIdList;
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param brokerage
     * @return
     */
    @Override
    public int updateDtoListAllById(Brokerage brokerage) {
        int updateDtoListAllById = brokerageDaoImpl.updateDtoListAllById(brokerage.getBrokerages());
        return updateDtoListAllById;
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param brokerage
     * @return
     */
    @Override
    public int updateById(Brokerage brokerage) {
        int updateById = brokerageDaoImpl.updateById(brokerage);
        return updateById;
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param brokerage
     * @return
     */
    @Override
    public int updateByIdList(Brokerage brokerage) {
        int updateByIdList = brokerageDaoImpl.updateByIdList(brokerage);
        return updateByIdList;
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param brokerage
     * @return
     */
    @Override
    public int updateDtoListById(Brokerage brokerage) {
        int updateDtoListById = brokerageDaoImpl.updateDtoListById(brokerage.getBrokerages());
        return updateDtoListById;
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     */
    @Override
    public Brokerage findById(Integer id) {
        Brokerage findById = brokerageDaoImpl.findById(id);
        return findById;
    }

    /**
     * 通过id数组查询
     * 
     * @param ids
     * @return
     */
    @Override
    public List<Brokerage> findByIds(Integer[] ids) {
        List<Brokerage> findByIds = brokerageDaoImpl.findByIds(ids);
        return findByIds;
    }

    /**
     * 通过对象查询id列表
     * 
     * @param brokerage
     * @return
     */
    @Override
    public List<Integer> findIdByDto(Brokerage brokerage) {
        List<Integer> findIdByDto = brokerageDaoImpl.findIdByDto(brokerage);
        return findIdByDto;
    }

    /**
     * 清空数据(物理清空)
     */
    @Override
    public int clear() {
        return brokerageDaoImpl.clear();
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws IOException 
     */
    @Override
    public Brokerage getInfo() throws IOException {
        Brokerage brokerage = CacheUtil.get(ConstantCache.BROKERAGE_KEY, Brokerage.class);
        if (StringUtil.isNotBlankObject(brokerage)) {
            return brokerage;
        }

        Brokerage findById = brokerageDaoImpl.findById(1);
        CacheUtil.set(ConstantCache.BROKERAGE_KEY, findById);
        return findById;
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param brokerage
     * @return
     * @throws IOException 
     */
    @Override
    public int update(Brokerage brokerage) throws IOException {
        brokerage.setId(1);
        Integer type = brokerage.getType();
        BigDecimal total = brokerage.getTotal();
        BigDecimal totalTop = brokerage.getTotalTop();
        if (StringUtil.isNotBlankObject(type)) {
            if (StringUtil.isBlankObject(total)) {
                throw new TransException("金额不能为空！");
            }
            int intValue = total.intValue();
            if (type == 1) {
                if (intValue <= 0) {
                    throw new TransException("佣金必须大于0！");
                }
            } else if (type == 2) {
                if (intValue < 1 || intValue > 99) {
                    throw new TransException("分佣百分比必须大于0且小于1");
                }
            }
            brokerage.setTotal(ArithUtil.intPennyToBigDollar(intValue));
            if (StringUtil.isNotBlankObject(totalTop)) {
                intValue = totalTop.intValue();
                if (intValue <= 0) {
                    throw new TransException("分佣百分比上限必须大于0！");
                }
                brokerage.setTotalTop(ArithUtil.intPennyToBigDollar(intValue));
            }
        } else {
            brokerage.setTotal(null);
            brokerage.setTotalTop(null);
        }
        brokerage.setUpdatedAt(DateUtil.nowDate());

        int updatePartialById = brokerageDaoImpl.updateById(brokerage);
        Brokerage findById = brokerageDaoImpl.findById(1);
        CacheUtil.set(ConstantCache.BROKERAGE_KEY, findById);
        return updatePartialById;
    }

}